class UsersIntro {
  constructor(mongoose) {
    this.mongoose = mongoose
    this.Schema = mongoose.Schema
    // AnimeIntro模板
    this.UsersIntro = new this.Schema({
      userName: String,
      userPwd: String,
      userId: String,
      loginDate: Date,
      identity: Number,
      avatar: String,
      isMember: Boolean,
      deadLine: Date,
      money: Number
    })
    this.modal = this.mongoose.model('User', this.UsersIntro)
  }
  // @paras1 查询条件 动漫名字数组
  // @paras2 查询结果过滤器
  // 返回查询结果
  UserFind(id) {
    return this.modal.find({ userId: id })
  }
  UsersFind(body) {
    return this.modal.find({ identity: body.identity }).sort({ userId: 1 }).skip(10 * body.page).limit(10)
  }
  beMember(body) {
    var startTime = new Date();
    var startDate = new Date(startTime);
    startDate.setDate(startDate.getDate() + 30 * body.month);

    this.modal.updateOne({ identity: 3 }, { $inc: { money: body.money } })
    return this.modal.updateOne({ userId: body.id }, { isMember: true, deadLine: startDate })
  }
  income(body) {
    return this.modal.updateOne({ userId: body.coachId }, { $inc: { money: body.money * 0.3 } })

  }
  income2(body) {
    return this.modal.updateOne({ userId: 'admin' }, { $inc: { money: body.money * 0.7 } })
  }

  addUser(body) {
    let order = new this.modal({
      userName: body.userName,
      userPwd: body.userPwd,
      userId: body.userId,
      loginDate: new Date(),
      identity: body.identity,
      avatar: body.avatar,
      isMember: false,
      deadLine: new Date(),
      money: 0,
    })
    return order.save()
  }
}

module.exports = UsersIntro
